package com.twinkle.framework.common.validator;

import com.twinkle.framework.common.exception.BusinessException;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Pattern;

public class EmailNumberValidator implements ConstraintValidator<EmailNumber, String>{
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9_\\.]+@163\\.com$";
    private static final Pattern PATTERN = Pattern.compile(EMAIL_REGEX);
    @Override
    public void initialize(EmailNumber constraintAnnotation) {
        // 这里进行一些初始化操作
    }

    @Override
    public boolean isValid(String emailNumber, ConstraintValidatorContext context) {
        // 先判断是否为null，避免空指针异常
        if (emailNumber == null) {
            return false;
        }
        return PATTERN.matcher(emailNumber).matches();
    }
}
